/*
 * @lc app=leetcode id=155 lang=java
 *
 * [155] Min Stack
 */

// @lc code=start

import java.util.Stack;

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minstack;
    public MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }
    
    public void push(int val) {
        //压栈
        stack.push(val);
        if(minstack.isEmpty()||val<=minstack.peek())
            minstack.push(val);
    }
    
    public void pop() {
        if(stack.isEmpty())
            return;
        else {
            int popVal = stack.pop();
            if(popVal == minstack.peek())
                minstack.pop();
            }
    }
    
    public int top() {
        if(stack.isEmpty())
            return -1;
        else 
            return stack.peek();
    }
    
    public int getMin() {
        if(minstack.isEmpty())
            return -1;
        else {
            return minstack.peek();
        }
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
// @lc code=end

